﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace EstateNuke.Data
{
    public partial class CustomerCommunication
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public CustomerCommunicationScheduledCallbackAlert LatestUnProcessedCustomerCommunicationScheduledCallbackAlert
        {
            get
            {
                return InternalCustomerCommunicationScheduledCallbackAlerts.FirstOrDefault(customerCommunicationScheduledCallbackAlert => customerCommunicationScheduledCallbackAlert.IsDeleted == false && customerCommunicationScheduledCallbackAlert.Processed == false);
            }
        }

        /// <summary>
        /// </summary>
        public IList<CustomerCommunicationScheduledCallbackAlert> CustomerCommunicationScheduledCallbackAlerts
        {
            get
            {
                return InternalCustomerCommunicationScheduledCallbackAlerts.Where(customerCommunicationScheduledCallbackAlert => customerCommunicationScheduledCallbackAlert.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Create & Remove CustomerCommunicationScheduledCallbackAlert

        /// <summary>
        /// </summary>
        /// <param name = "alertType"></param>
        /// <param name = "modifiedUser"></param>
        public CustomerCommunicationScheduledCallbackAlert CreateCustomerCommunicationScheduledCallbackAlert(AlertType alertType, User modifiedUser)
        {
            if (alertType == null)
            {
                throw new ArgumentNullException("alertType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (alertType.Id != 10)
            {
                throw new ArgumentException("alertType");
            }
            if (!CustomerReference.IsLoaded)
            {
                CustomerReference.Load(MergeOption.PreserveChanges);
            }
            if (!Customer.ProjectReference.IsLoaded)
            {
                Customer.ProjectReference.Load(MergeOption.PreserveChanges);
            }
            var reminderDays = alertType.DefaultReminderDays.HasValue ? alertType.DefaultReminderDays : 0;
            var alertTypeReminderDays = Customer.Project.AlertTypeReminderDays;
            var customerRequirement = Customer.CustomerRequirement;
            if (customerRequirement != null)
            {
                if (alertTypeReminderDays != null && alertTypeReminderDays.ContainsKey(alertType) && alertTypeReminderDays[alertType] != null)
                {
                    switch (customerRequirement.RequirementLevelId)
                    {
                        case 1:
                            if (alertTypeReminderDays[alertType].Item2.HasValue)
                            {
                                reminderDays = alertTypeReminderDays[alertType].Item2;
                            }
                            break;
                        case 2:
                            if (alertTypeReminderDays[alertType].Item3.HasValue)
                            {
                                reminderDays = alertTypeReminderDays[alertType].Item3;
                            }
                            break;
                        case 3:
                            if (alertTypeReminderDays[alertType].Item4.HasValue)
                            {
                                reminderDays = alertTypeReminderDays[alertType].Item4;
                            }
                            break;
                        default:
                            if (alertTypeReminderDays[alertType].Item1.HasValue)
                            {
                                reminderDays = alertTypeReminderDays[alertType].Item1;
                            }
                            break;
                    }
                }
            }
            else
            {
                if (alertTypeReminderDays != null && alertTypeReminderDays.ContainsKey(alertType) && alertTypeReminderDays[alertType] != null && alertTypeReminderDays[alertType].Item1.HasValue)
                {
                    reminderDays = alertTypeReminderDays[alertType].Item1;
                }
            }
            if (reminderDays != 0)
            {
                var customerCommunicationScheduledCallbackAlert = InternalCustomerCommunicationScheduledCallbackAlerts.FirstOrDefault(item => item.IsDeleted == false && item.Processed == false);
                if (customerCommunicationScheduledCallbackAlert == null)
                {
                    customerCommunicationScheduledCallbackAlert = new CustomerCommunicationScheduledCallbackAlert
                                                                      {
                                                                          RowId = Guid.NewGuid(),
                                                                          CreatedDate = DateTime.Now,
                                                                      };
                    InternalCustomerCommunicationScheduledCallbackAlerts.Add(customerCommunicationScheduledCallbackAlert);
                }
                customerCommunicationScheduledCallbackAlert.IsDeleted = false;
                customerCommunicationScheduledCallbackAlert.ModifiedDate = DateTime.Now;
                customerCommunicationScheduledCallbackAlert.ModifiedByUser = modifiedUser;
                customerCommunicationScheduledCallbackAlert.AlertType = alertType;
                customerCommunicationScheduledCallbackAlert.RemindDate = CommunicationDate.AddDays((double) reminderDays);
                return customerCommunicationScheduledCallbackAlert;
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name = "customerCommunicationScheduledCallbackAlert"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveCustomerCommunicationScheduledCallbackAlert(CustomerCommunicationScheduledCallbackAlert customerCommunicationScheduledCallbackAlert, User modifiedUser)
        {
            if (customerCommunicationScheduledCallbackAlert == null)
            {
                throw new ArgumentNullException("customerCommunicationScheduledCallbackAlert");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalCustomerCommunicationScheduledCallbackAlerts.Contains(customerCommunicationScheduledCallbackAlert))
            {
                customerCommunicationScheduledCallbackAlert.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<CustomerCommunicationScheduledCallbackAlert>) CustomerCommunicationScheduledCallbackAlerts).ForEach(customerCommunicationScheduledCallbackAlert => customerCommunicationScheduledCallbackAlert.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}